﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.BusinessLogicLayer;
public partial class DeviceUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("login.aspx");
            }
            /*初始化设备类别下拉框*/
            this.DeviceType.DataSource = (new DeviceType()).GetDeviceType();
            this.DeviceType.DataTextField = "deviceTypeName";
            this.DeviceType.DataValueField = "deviceTypeId";
            this.DeviceType.DataBind(); ;
            /*根据设备编号得到设备各个字段的信息并添入个控件中*/
            dvId.Value = Request.QueryString["deviceId"];
            DeviceInfo device = new DeviceInfo();
            device.GetDeviceInfo(Int32.Parse(dvId.Value));
            this.DeviceName.Text = device.DeviceName;
            //this.DeviceType.Text = (new DeviceType()).GetDeviceTypeName(device.DeviceTypeId);
            this.DeviceType.SelectedValue = device.DeviceTypeId.ToString();
            //this.DeviceSign.Text = device.DeviceSign;
            this.DeviceModel.Text = device.DeviceModel;
            this.DeviceSerialNumber.Text = device.DeviceSerialNumber;
            if (device.DeviceSerialNumber != "")
            {
                ViewState["sn"] = device.DeviceSerialNumber;
            }
            else
            {
                btnLover.Visible = false;
                btnLook.Visible = false;
            }
            //this.DeviceNumber.Text = device.DeviceNumber;
            this.DeviceDpt.Text = device.DepartmentName;
            this.DeviceUser.Text = device.DeviceUser;
            this.txtCPU.Text = device.DeviceCPU;
            this.txtMemory.Text = device.DeviceMemory;
            this.txtHardisk.Text = device.DeviceHarddisk;
            this.txtIPAddress.Text = device.IPAddress;
            this.txtWindowNumber.Text = device.WindowNumber;
            //this.DeviceOutDate.Text = device.DeviceOutDate.ToShortDateString();
            //this.DevicePurchaseTime.Text = device.DevicePurchaseTime.ToShortDateString();
            //if (device.DeviceImagePath != "")
            //{
            //    this.DeviceImagePath.Text = device.DeviceImagePath;
            //    this.DeviceImage.ImageUrl = device.DeviceImagePath;
            //}
            //else
            //    this.DeviceImage.ImageUrl = "images\\NoImage.jpg";

            this.DeviceMemo.Text = device.DeviceMemo;

            DataSet ds = (new reservedMan()).GetList(1, "DeviceSerialNumber = '" + device.DeviceSerialNumber + "'", "reservedTime desc");
            if (ds.Tables[0].Rows.Count != 0) { lbdate.InnerText = ds.Tables[0].Rows[0]["reservedTime"].ToString().Split(' ')[0]; }
            else { lbdate.InnerText = "无备份信息"; }
        }

    }
    protected void Btn_Update_Click(object sender, EventArgs e)
    {
        DeviceInfo device = new DeviceInfo();
        device.DeviceId = Int32.Parse(dvId.Value);
        device.DeviceName = this.DeviceName.Text;
        device.DeviceTypeId = Int32.Parse(this.DeviceType.SelectedValue);
        //device.DeviceSign = this.DeviceSign.Text;
        device.DeviceModel = this.DeviceModel.Text;
        device.DeviceSerialNumber = this.DeviceSerialNumber.Text;
        //device.DeviceImagePath = this.DeviceImagePath.Text;
        device.DeviceUser = this.DeviceUser.Text;
        device.DepartmentName = this.DeviceDpt.SelectedValue;
        device.DeviceCPU = this.txtCPU.Text;
        device.DeviceHarddisk = this.txtHardisk.Text;
        device.DeviceMemory = this.txtMemory.Text;
        device.IPAddress = this.txtIPAddress.Text;
        device.WindowNumber = this.txtWindowNumber.Text;
        //device.DeviceNumber = this.DeviceNumber.Text;
        //if(this.DeviceOutDate.Text != "")
        //    device.DeviceOutDate = Convert.ToDateTime(this.DeviceOutDate.Text);
        //device.DevicePurchaseTime = Convert.ToDateTime(this.DevicePurchaseTime.Text);
        device.DeviceMemo = this.DeviceMemo.Text;

        if (device.DeviceInfoUpdate(Server.MapPath("")))
            Response.Write("<script>alert('设备信息更新成功!');</script>");
        else
            Response.Write("<script>alert('" + device.ErrMessage + "');</script>");


    }
    //protected void Btn_Upload_Click(object sender, EventArgs e)
    //{
    //    /*如果用户上传了新的图片文件*/
    //    if (this.DeviceImageUpload.PostedFile.ContentLength > 0)
    //    {
    //        /*验证上传的文件格式，只能为gif和jpeg格式*/
    //        string mimeType = this.DeviceImageUpload.PostedFile.ContentType;
    //        if (String.Compare(mimeType, "image/gif", true) == 0 || String.Compare(mimeType, "image/pjpeg", true) == 0)
    //        {
    //            this.DeviceImagePath.Text = "上传文件中....";
    //            string extFileString = System.IO.Path.GetExtension(this.DeviceImageUpload.PostedFile.FileName); /*获取文件扩展名*/
    //            string saveFileName = this.MakeFileName(extFileString); /*根据扩展名生成文件名*/
    //            string imagePath = "deviceImg\\" + saveFileName;/*图片路径*/
    //            this.DeviceImageUpload.PostedFile.SaveAs(Server.MapPath(imagePath));
    //            this.DeviceImage.ImageUrl = imagePath;
    //            this.DeviceImagePath.Text = imagePath;
    //        }
    //        else
    //        {
    //            Response.Write("<script>alert('上传文件格式不正确!');</script>");
    //        }
    //    }
    //}

    /*根据当前系统时间生成一个文件名*/
    private string MakeFileName(string exeFileString)
    {
        System.DateTime now = System.DateTime.Now;
        int year = now.Year;
        int month = now.Month;
        int day = now.Day;
        int hour = now.Hour;
        int minute = now.Minute;
        int second = now.Second;

        string yearString = year.ToString();
        string monthString = month < 10 ? ("0" + month) : month.ToString();
        string dayString = day < 10 ? ("0" + day) : day.ToString();
        string hourString = hour < 10 ? ("0" + hour) : hour.ToString();
        string minuteString = minute < 10 ? ("0" + minute) : minute.ToString();
        string secondString = second < 10 ? ("0" + second) : second.ToString();

        /*根据当前时间的年月日时分秒生成文件名*/
        string fileName = yearString + monthString + dayString + hourString + minuteString + secondString + exeFileString;
        return fileName;
    }
    protected void Btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("DeviceManage.aspx");
    }

    protected void btnLover_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintRecordRegister.aspx?sn=" + ViewState["sn"].ToString());
    }

    protected void btnLook_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintRecordList.aspx?sn=" + ViewState["sn"].ToString());
    }
}
